package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.dto.HisRoomDto;
import com.woniu.dto.PageDTO;
import com.woniu.exception.DIYException;
import com.woniu.model.HisRoom;
import com.woniu.mapper.HisRoomMapper;
import com.woniu.param.HisRoomAddParam;
import com.woniu.param.HisRoomParam;
import com.woniu.param.PageParam;
import com.woniu.service.IHisRoomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.util.CopyBeanUtil;
import com.woniu.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-11
 */
@Service
public class HisRoomServiceImpl extends ServiceImpl<HisRoomMapper, HisRoom> implements IHisRoomService {
    @Resource
    private HisRoomMapper hisRoomMapper;


    @Override
    public Result<HisRoomDto> addHisRoom(HisRoomAddParam hisRoomAddParam) {
        if (hisRoomAddParam==null){new DIYException(-9999,"服务器异常，新增数据对象为空");}
        HisRoom hisRoom = new HisRoom();
        BeanUtils.copyProperties(hisRoomAddParam,hisRoom);
        hisRoomMapper.insert(hisRoom);
        HisRoomDto hisRoomDto = new HisRoomDto();
        BeanUtils.copyProperties(hisRoom,hisRoomDto);
        return Result.bulid(hisRoomDto);
    }

    @Override
    public Result<Boolean> deleteHisRoomById(ArrayList<Integer> id) {
        if(id==null){new DIYException(-9999,"服务器异常，病房新增数据为空");}
        int i = hisRoomMapper.deleteBatchIds(id);
        return Result.bulid(i==0?new Boolean(false):new Boolean(true));
    }

    @Override
    public Result<HisRoomDto> updateHisRoom(HisRoomParam hisRoomParam) {
        if (hisRoomParam==null){new DIYException(-9999,"hisRoomParam为空");}
        HisRoom hisRoom = new HisRoom();
        HisRoomDto hisRoomDto = new HisRoomDto();
        BeanUtils.copyProperties(hisRoomParam,hisRoomDto);
        BeanUtils.copyProperties(hisRoomParam,hisRoom);
        hisRoomMapper.updateById(hisRoom);
        return Result.bulid(hisRoomDto);
    }

    @Override
    public Result<PageDTO<HisRoomDto>> getHisRoomById(HisRoomParam hisRoomParam) {
        if (hisRoomParam==null){new DIYException(-9999,"hisRoomParam为空");}
        QueryWrapper<HisRoom> wrapper = new QueryWrapper<>();
        if (hisRoomParam.getRoomId()!=null){
            wrapper.eq("room_id",hisRoomParam.getRoomId());
        }
        if (hisRoomParam.getRoomNumber()!=null){
            wrapper.eq("room_number",hisRoomParam.getRoomNumber());
        }
        if (hisRoomParam.getOfficeId()!=null){
            wrapper.eq("office_id",hisRoomParam.getOfficeId());
        }
        if (hisRoomParam.getOfficeName()!=null){
            wrapper.eq("office_name",hisRoomParam.getOfficeName());
        }
        if (hisRoomParam.getBedNumber()!=null){
            wrapper.eq("bed_number",hisRoomParam.getBedNumber());
        }
        if (hisRoomParam.getBedFree()!=null){
            wrapper.eq("bed_free",hisRoomParam.getBedFree());
        }
        if(hisRoomParam.getPageParam()==null){
            PageParam pageParam = new PageParam();
            pageParam.setPage(0L);
            pageParam.setPageSize(100L);
            hisRoomParam.setPageParam(pageParam);
        }
        Page<HisRoom> page = page(new Page<HisRoom>(hisRoomParam.getPageParam().getPage(),hisRoomParam.getPageParam().getPageSize()),wrapper);
        List<HisRoomDto> hisRoomDtos = CopyBeanUtil.copyBean(page.getRecords(), HisRoomDto.class);
        return Result.bulid(PageDTO.bulid(page,hisRoomDtos));
    }

    @Override
    public Result<PageDTO<HisRoomDto>> getAllRoom(PageParam pageParam) {
        QueryWrapper<HisRoom> queryWrapper = new QueryWrapper<>();
        Page<HisRoom> page = page(new Page<HisRoom>(pageParam.getPage(),pageParam.getPageSize()),queryWrapper);
        List<HisRoomDto> hisRoomDtos = CopyBeanUtil.copyBean(page.getRecords(), HisRoomDto.class);
        return Result.bulid(PageDTO.bulid(page,hisRoomDtos));
    }
}
